package com.atguigu.gmall.item.controller;

/**
 * @author sunjipu
 * @create 2020-04-14 15:17
 */

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品列表接口
 * </p>
 *
 * @author qy
 */
@Controller
@RequestMapping
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 列表搜索
     * @param searchParam
     * @return
     */
    @GetMapping("list.html")
    public String search(SearchParam searchParam, Model model) {
        Result<Map> result = listFeignClient.list(searchParam);
        model.addAllAttributes(result.getData());
        //拼接url
        String urlParam = makeUrlParam(searchParam);
        //处理品牌条件回显
        String trademarkParam = this.makeTrademark(searchParam.getTrademark());
        //处理平台属性条件回显
        List<Map<String, String>> propsParamList = this.makeProps(searchParam.getProps());
        //处理排序
        Map<String,Object> orderMap = this.dealOrder(searchParam.getOrder());


        model.addAttribute("searchParam",searchParam);
        model.addAttribute("urlParam",urlParam);
        model.addAttribute("trademarkParam",trademarkParam);
        model.addAttribute("propsParamList",propsParamList);
        model.addAttribute("orderMap",orderMap);
        return "list/index";
    }


    /**
     * 拼接url
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder urlParam = new StringBuilder();
        // 判断skuName 是否为空
        if (searchParam.getKeyword()!=null){
            urlParam.append("keyword").append("=").append(searchParam.getKeyword());
        }
        // 判断分类Id
        if (searchParam.getCategory1Id()!=null){
            urlParam.append("category1Id").append("=").append(searchParam.getCategory1Id());
        }
        if (searchParam.getCategory2Id()!=null){
            urlParam.append("category2Id").append("=").append(searchParam.getCategory2Id());
        }
        if (searchParam.getCategory3Id()!=null){
            urlParam.append("category3Id").append("=").append(searchParam.getCategory3Id());
        }
        //处理品牌
        if (!StringUtils.isEmpty(searchParam.getTrademark())){
            if (urlParam.length() > 0){
                urlParam.append("&").append("trademark").append("=").append(searchParam.getTrademark());
            } else {
                urlParam.append("trademark").append("=").append(searchParam.getTrademark());
            }
        }
        // 判断平台属性值
        if (null != searchParam.getProps()){
            for (String prop : searchParam.getProps()) {
                if (urlParam.length() > 0){
                    urlParam.append("&");
                }
                urlParam.append("props").append("=").append(prop);
            }
        }
        return "list.html?" + urlParam.toString();
    }




    /**
     * 处理品牌条件回显
     * @param trademark
     * @return
     */
    private String makeTrademark(String trademark) {
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = StringUtils.split(trademark, ":");
            if (null !=split && split.length == 2) {
                return "品牌：" + split[1];
            }
        }
        return "";
    }

    /**
     * 处理平台属性条件回显
     * @param props
     * @return
     */
    private List<Map<String, String>> makeProps(String[] props) {
        List<Map<String, String>> propsParamList = new ArrayList<>();
        if (props != null && props.length != 0) {
            for (String prop : props) {
                String[] split  = prop.split(":");
                if (null !=split && split.length == 3) {
                    Map<String, String > propMap = new HashMap<String, String>(){{
                        put("attrId",split[0]);
                        put("attrValue",split[1]);
                        put("attrName",split[2]);
                    }};
                    propsParamList.add(propMap);
                }
            }
        }
        return propsParamList;
    }

    /**
     * 处理排序
     * @param order
     * @return
     */
    private Map<String, Object> dealOrder(String order) {
        Map<String,Object> orderMap = new HashMap<>();
        if(!StringUtils.isEmpty(order)) {
            String[] split = StringUtils.split(order, ":");
            if (null !=split && split.length == 2) {
                orderMap.put("type", split[0]);
                orderMap.put("sort", split[1]);
            }
        } else {
            orderMap.put("type", "1");
            orderMap.put("sort", "asc");
        }
        return orderMap;
    }



}